import {getSpuInfo} from '../../../../api/storeDetail.js'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    storeInfo: {
      type: Object,
      value: null,
      observer(service) {
        if (service.typeList && service.typeList.length) {
          this.setData({
            storeInfo: service,
            activeShopInfo: service.typeList[0]
          }, () => {
            this.getShopListInfo()
          })
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    sideBarIndex: 0,
    scrollTop: 0,
    // categories: [],
    navbarHeight: 38,
    storeInfo: null,
    shopList: [],
    activeShopInfo: null,
    pageInfo: {
      pageNo: 1,
      pageSize: 10
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSideBarChange(e) {
      const {
        value
      } = e.detail;

      this.setData({
        sideBarIndex: value,
        scrollTop: 0,
        activeShopInfo: this.data.storeInfo.typeList[value]
      }, ()=> {
        this.getShopListInfo()
      });
    },
    async getShopListInfo(fresh = false) {
      if (fresh) {
        this.data.pageInfo.pageNo = 1
      }
      let params = {
        ...this.data.pageInfo,
        categoryId: this.data.activeShopInfo.id,
        shopId:	this.data.storeInfo.id
      }
      const data = await getSpuInfo(params)
      data.list && data.list.map(t => {
        t.price = t.price && (t.price / 100).toFixed(2)
        return t
      })
      console.log('data', data);
      this.setData({
        shopList: fresh ? this.data.shopList.concat(data.list) : data.list
      }, ()=> {
        console.log('111', this.data.shopList);
      })
    },
    runGoodsDetail(e) {
      const {item} = e.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/goods/details/index?goodsId=${item.id}`,
      });
    },
    onReachBottom() {
      this.data.pageInfo.pageNo++;
      this.getShopListInfo();
    }
  }
})